Imports System.IO
Public Class FormLockDemo
    Dim FileSt As FileStream
    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        FileSt = New FileStream("C:\Sach.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim Reader As New StreamReader(FileSt)
        txtContent.Text = Reader.ReadToEnd()

    End Sub

    Private Sub btnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLock.Click
        Try
            FileSt.Lock(0, 5)
            txtStatus.AppendText("File đã khóa" & vbCrLf)
        Catch Ex As Exception
            txtStatus.AppendText(Ex.Message & vbCrLf)
        End Try
    End Sub

    Private Sub btnOpenLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenLock.Click
        Try
            FileSt.Unlock(0, 5)
            txtStatus.AppendText("File mở khóa" & vbCrLf)
        Catch Ex As Exception
            txtStatus.AppendText(Ex.Message & vbCrLf)
        End Try
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim Values As Byte()
        Values = New Byte() {1, 2, 3, 4, 5}

        Try
            FileSt.Seek(0, SeekOrigin.Begin)
            FileSt.Write(Values, 0, 5)
            txtStatus.AppendText("File cập nhật thành công")
        Catch Ex As Exception
            txtStatus.AppendText(Ex.Message & vbCrLf)
        End Try
    End Sub
End Class
